<template>
  <div>
  </div>
</template>

<script>
  import api from 'api/api'
  import common from "api/common";
  import {mapState} from 'vuex'

  export default {
    components: {},
    props: {},
    data() {
      return {
      }
    },
    watch: {},
    computed:{
      ...mapState({
        latPath:state=>state.comm.path,
        page_access_token:state=>state.user.page_access_token,
      })
    },
    methods: {
      getOpenId(code) {
        let path = sessionStorage.getItem('lastPath')
        if(this.page_access_token){
          console.log('path='+path)
          return window.location.replace(window.location.origin+'/index.html#'+path)
        }
        console.log(code)
        this.$store.commit("user/SET_WX_CODE", code)
        let tencentId = sessionStorage.getItem('tencentId')
        const tencent = window.global.TencentList[tencentId]
        let type = tencent.type
        api.fetchSimple('/GhPush/GetoAuth2Token',{code:code,type:type}).then(r=>{
          this.$store.commit("user/SET_OPENID", {openid:r.openid,token:r.token});
          // this.$router.replace(path)
          window.location.replace(window.location.origin+ '/index.html#'+path)
          // this.$router.replace('/static/user')
        })
      },
      getAccessToken(){
        let urlNow=encodeURIComponent(window.location.href);
        // const {appID, appsecret} = api.config
        let tencentId=sessionStorage.getItem('tencentId')
        const tencent=window.global.TencentList[tencentId]
        let appID=tencent.appID
        let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appID + '&redirect_uri=' + urlNow + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
        window.location.replace(url);
      },
      init(){
        let code=common.getQueryString("code");//获取url参数code
        if(code){ //拿到code， code传递给后台接口换取opend
          this.getOpenId(code)
        }else{
          this.getAccessToken()
        }
      }
    },
    created() {
      let tencentId = sessionStorage.getItem('tencentId')
      const tencent = window.global.TencentList[tencentId]
      this.init()
    },
    mounted() {
     /* window.onload = function () {
        setTimeout(() => {
          window.addEventListener("popstate", function (e) {
          });
        }, 500)
      }*/
    }
  }
</script>
<style  lang="stylus" rel="stylesheet/stylus">
</style>
